﻿using System;

namespace System
{
    public static class DateTimeHelper
    {
        /// <summary>
        /// 时间格式化(yyyy-MM-dd HH:mm:ss)
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

        /// <summary>
        /// 时间格式化(yyyy-MM-dd HH:mm:ss.fff)
        /// </summary>
        public static string yyyy_MM_dd_HH_mm_ss_fff = "yyyy-MM-dd HH:mm:ss.fff";

        /// <summary>
        /// 时间格式化(yyyyMMddHHmmss)
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string yyyyMMddHHmmss = "yyyyMMddHHmmss";

        /// <summary>
        /// 时间格式化(yyyyMMdd)
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string yyyyMMdd = "yyyyMMdd";

        /// <summary>
        /// 时间格式化(yyyy-MM-dd)
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string yyyy_MM_dd = "yyyy-MM-dd";

        /// <summary>
        /// 时间格式化(yy-MM-dd)
        /// </summary>
        /// <param name="Size">初始文件大小</param>
        /// <returns></returns>
        public static string yy_MM_dd = "yy-MM-dd";

        /// <summary>
        /// 获取1970年时间
        /// </summary>
        public static readonly DateTime UtcTime1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        /// <summary>
        /// 将录像的时间戳(1970.1.1 00:00:00起)转换为本地时间
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public static DateTime FromRecordUtcPos(long pos)
        {
            var tp = TimeSpan.FromMilliseconds(pos);
            var dt = UtcTime1970.Add(tp).ToLocalTime();
            return dt;
        }

        /// <summary>
        /// 将本地时间转换为(从1970.1.1 00:00:00起的)录像时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ToRecordUtcPos(DateTime dt)
        {
            var pos = (dt.ToUniversalTime().Ticks - UtcTime1970.Ticks) / TimeSpan.TicksPerMillisecond;
            return pos;
        }

        /// <summary>
        /// 获取录像时间字符串
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string GetRecordTimeString(DateTime time)
        {
            return $"{time.Year:d4}{time.Month:d2}{time.Day:d2}{time.Hour:d2}{time.Minute:d2}{time.Second:d2}{time.Millisecond:d3}";
        }

        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <param name="isClearZero">是否去除0000</param>  
        /// <returns>long</returns>  
        public static long ToDateTimeTick(this DateTime time, bool isClearZero = true)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = isClearZero ?
                (time.Ticks - startTime.Ticks) / 10000 :
                (time.Ticks - startTime.Ticks);
            return t;
        }

        /// <summary>
        /// 日期转换
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static DateTime GetTimeFromRecordTimeString(string time)
        {
            try
            {
                if (string.IsNullOrEmpty(time))
                    return DateTime.MinValue;

                if (time.Length == 14)
                    return DateTime.ParseExact(time, "yyyyMMddHHmmss", null);

                var year = time.SubstringEx(0, 4);
                var month = time.SubstringEx(4, 2);
                var day = time.SubstringEx(6, 2);
                var hour = time.SubstringEx(8, 2);
                var minute = time.SubstringEx(10, 2);
                var second = time.SubstringEx(12, 2);
                var millionSecond = time.SubstringEx(14, 3);
                int y, M, d, h, m, s, ms;
                if (!int.TryParse(year, out y) ||
                    !int.TryParse(month, out M) ||
                    !int.TryParse(day, out d) ||
                    !int.TryParse(hour, out h) ||
                    !int.TryParse(minute, out m) ||
                    !int.TryParse(second, out s) ||
                    !int.TryParse(millionSecond, out ms))
                    return DateTime.MinValue;
                return new DateTime(y, M, d, h, m, s, ms);
            }
            catch (Exception)
            {
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// 去除毫秒部分
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime CutMillisecond(this DateTime dt)
        {
            return new DateTime(dt.Ticks - (dt.Ticks % TimeSpan.TicksPerSecond), dt.Kind);
        }

        /// <summary>
        /// 获取当前时间(yyyyMMdd)
        /// </summary>
        public static string Now_yyyyMMdd
        {
            get
            {
                return DateTime.Now.ToString("yyyyMMdd");
            }
        }

        /// <summary>
        /// 获取当前时间(yyyyMMddHHmmss)
        /// </summary>
        public static string Now_yyyyMMddHHmmss
        {
            get
            {
                return DateTime.Now.ToString("yyyyMMddHHmmss");
            }
        }
        /// <summary>
        /// 获取当前时间(yyyy-MM-dd)
        /// </summary>
        public static string Now_yyyy_MM_dd
        {
            get
            {
                return DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

        /// <summary>
        /// 获取当前时间(yyyy-MM-dd HH:mm:ss)
        /// </summary>
        public static string Now_yyyy_MM_dd_HH_mm_ss
        {
            get
            {
                return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

    }
}
